//
//  YKFarmViewModel.m
//  YK_BaseMediator
//
//  Created by edward on 2021/3/11.
//  Copyright © 2021 Edward. All rights reserved.
//

#import "YKFarmViewModel.h"

@interface YKFarmViewModel ()

///
@property(nonatomic,copy) NSString *userId;
///
@property(nonatomic,strong,readwrite) RACCommand *farmListCommand;
///
@property(nonatomic,strong,readwrite) RACCommand *addfarmCommand;
///
@property(nonatomic,strong,readwrite) NSMutableArray *farmListArray;

#pragma mark -----------data------------

///
@property(nonatomic,strong,readwrite) NSMutableArray *farmDataListArray;
///
@property(nonatomic,strong,readwrite) RACCommand *farmDataListCommand;
///
@property(nonatomic,strong,readwrite) RACCommand *deletefarmDataListCommand;
///
@property(nonatomic,strong,readwrite) RACCommand *addfarmDataListCommand;
///
@property(nonatomic,strong,readwrite) RACSubject *reloadSubject;

///
@property(nonatomic,strong,readwrite) RACCommand *farmEveryDayCommand;
///
@property(nonatomic,strong) NSDictionary *everyExName;
///
@property(nonatomic,strong) NSMutableDictionary *farmLocalEveryDayDic;
/***/
@property (nonatomic, strong,readwrite) RACCommand *saveEveryDayComm;

///
@property(nonatomic,strong,readwrite) RACCommand *farmWaterCommand;
/***/
@property (nonatomic, strong,readwrite) RACCommand *saveWaterComm;
///
@property(nonatomic,strong) NSMutableDictionary *farmLocalWaterDic;
///
@property(nonatomic,strong,readwrite) RACCommand *farmFoodCommand;
/***/
@property (nonatomic, strong,readwrite) RACCommand *saveFoodComm;
/***/
@property (nonatomic, strong,readwrite) RACCommand *deleteFoodComm;
#pragma mark -----------drug------------
///
@property(nonatomic,strong,readwrite) RACCommand *farmDrugCommand;
/***/
@property (nonatomic, strong,readwrite) RACCommand *saveDrugComm;
/***/
@property (nonatomic, strong,readwrite) RACCommand *deleteDrugComm;
@end

@implementation YKFarmViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[RACSignal merge:@[self.farmListCommand.errors,self.farmDataListCommand.errors,self.farmEveryDayCommand.errors,self.saveEveryDayComm.errors,self.farmWaterCommand.errors,self.saveWaterComm.errors,self.farmFoodCommand.errors,self.saveFoodComm.errors,self.deleteFoodComm.errors,self.deletefarmDataListCommand.errors,self.addfarmCommand.errors,self.farmDrugCommand.errors,self.saveDrugComm.errors,self.deleteDrugComm.errors]] subscribe:self.errorSubject];
        self.netWorking.prefixUrl = @"http://106.55.12.108/";
        @weakify(self);
        [self.netWorking setDynamicParamsConfig:^NSDictionary * _Nonnull(YKNetworkRequest * _Nonnull request) {
            @strongify(self);
            return @{@"page":@(self.page).stringValue,@"size":@(self.size).stringValue};
        }];
        [self.netWorking setHandleResponse:^NSError * _Nonnull(YKNetworkResponse * _Nonnull response, YKNetworkRequest * _Nonnull request) {
            NSDictionary * getDict = response.rawData;
            if([getDict isKindOfClass:NSDictionary.class] && [getDict.allKeys containsObject:@"code"]&&[getDict.allKeys containsObject:@"data"]&&[getDict.allKeys containsObject:@"msg"])
            {
                NSInteger code = [getDict[@"code"] integerValue];
                response.rawData = getDict[@"data"];
                if (code == 404 || code == 500) {
                    NSError *err = [NSError errorWithDomain:@"YKFarmViewModel" code:code userInfo:@{
                                   NSLocalizedDescriptionKey:getDict[@"data"],
                                   NSLocalizedFailureReasonErrorKey:getDict[@"data"],
                                   }];
                    return err;
                }else
                {
                    return nil;
                }
                
            }else{
                
                NSError *err = [NSError errorWithDomain:@"YKFarmViewModel" code:-1 userInfo:@{
                               NSLocalizedDescriptionKey:@"网络请求错误",
                               NSLocalizedFailureReasonErrorKey:@"网络请求错误",
                               }];
                return err;
            }
            return nil;
        }];
    }
    return self;
}

- (instancetype)initWithUserId:(NSString *)userId
{
    if(self = [self init])
    {
        self.userId = userId;
    }
    return self;
}

- (RACCommand *)farmListCommand
{
    if(!_farmListCommand)
    {
        @weakify(self);
        _farmListCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            return  [self.netWorking.get(@"farm/index/getFarmList").params(@{@"userId":self.userId}).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
//                [self handlePageData:self.farmListArray newDatas:value];
                self.farmListArray = value;
                return @(YES);
            }];
            
        }];
    }
    return _farmListCommand;
}
- (RACCommand *)addfarmCommand
{
    if(!_addfarmCommand)
    {
        @weakify(self);
        _addfarmCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            return  [self.netWorking.get(@"farm/index/createFarm").params(input).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                //NSArray<MTDynamicDataModel *> *list = [MTDynamicDataModel mj_objectArrayWithKeyValuesArray:value[@"list"]];
                //[self handlePageData:self.dataList newDatas:list];
                return @(YES);
            }];
            
        }];
    }
    return _addfarmCommand;
}

- (RACCommand *)farmDataListCommand
{
    if(!_farmDataListCommand)
    {
        @weakify(self);
        _farmDataListCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            NSString *farmDataId = input?:@"";
            return  [self.netWorking.get(@"farm/index/FarmDataList").params(@{@"farmlistId":farmDataId}).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                [self handlePageData:self.farmDataListArray newDatas:value];
                return @(YES);
            }];
            
        }];
    }
    return _farmDataListCommand;
}

- (RACCommand *)deletefarmDataListCommand
{
    if(!_deletefarmDataListCommand)
    {
        @weakify(self);
        _deletefarmDataListCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            NSString *farmId = input?:@"";
            return  [self.netWorking.get(@"farm/index/deleteFarmData").params(@{@"farmId":farmId}).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                //NSArray<MTDynamicDataModel *> *list = [MTDynamicDataModel mj_objectArrayWithKeyValuesArray:value[@"list"]];
                //[self handlePageData:self.dataList newDatas:list];
                return @(YES);
            }];
            
        }];
    }
    return _deletefarmDataListCommand;
}

- (RACCommand *)addfarmDataListCommand
{
    if(!_addfarmDataListCommand)
    {
        @weakify(self);
        _addfarmDataListCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            return  [self.netWorking.get(@"farm/index/createFarmData").params(input).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                //NSArray<MTDynamicDataModel *> *list = [MTDynamicDataModel mj_objectArrayWithKeyValuesArray:value[@"list"]];
                //[self handlePageData:self.dataList newDatas:list];
                return @(YES);
            }];
            
        }];
    }
    return _addfarmDataListCommand;
}

- (RACCommand *)farmEveryDayCommand
{
    if(!_farmEveryDayCommand)
    {
        @weakify(self);
        _farmEveryDayCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            NSString *farmId = input?:@"";
            return  [self.netWorking.get(@"farm/detail/everyDay").params(@{@"farmId":farmId}).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                self.farmEveryDayDic = [value mutableCopy];
                [self.farmEveryDayDic removeObjectForKey:@"Id"];
                [self.farmEveryDayDic removeObjectForKey:@"farmId"];
                self.farmLocalEveryDayDic = [value mutableCopy];
                [self.farmLocalEveryDayDic setValue:value[@"Id"] forKey:@"everyDayId"];
                return @(YES);
            }];
            
        }];
    }
    return _farmEveryDayCommand;
}

- (RACCommand *)saveEveryDayComm
{
    if(!_saveEveryDayComm)
    {
        @weakify(self);
        _saveEveryDayComm = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            return  [self.netWorking.get(@"farm/detail/motifiEveryDay").params(self.farmLocalEveryDayDic).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                self.farmEveryDayDic = [value mutableCopy];
                [self.farmEveryDayDic removeObjectForKey:@"Id"];
                [self.farmEveryDayDic removeObjectForKey:@"farmId"];
                self.farmLocalEveryDayDic = [value mutableCopy];
                [self.farmLocalEveryDayDic setValue:value[@"Id"] forKey:@"everyDayId"];
                return @(YES);
            }];
            
        }];
    }
    return _saveEveryDayComm;
}

- (RACCommand *)farmWaterCommand
{
    if(!_farmWaterCommand)
    {
        @weakify(self);
        _farmWaterCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            NSString *farmId = input?:@"";
            return  [self.netWorking.get(@"farm/detail/checkWater").params(@{@"farmId":farmId}).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                //NSArray<MTDynamicDataModel *> *list = [MTDynamicDataModel mj_objectArrayWithKeyValuesArray:value[@"list"]];
                //[self handlePageData:self.dataList newDatas:list];
                self.farmWaterDic = [value mutableCopy];
                [self.farmWaterDic removeObjectForKey:@"Id"];
                [self.farmWaterDic removeObjectForKey:@"farmId"];
                self.farmLocalWaterDic = [value mutableCopy];
                [self.farmLocalWaterDic setValue:value[@"Id"] forKey:@"checkId"];
                return @(YES);
            }];
            
        }];
    }
    return _farmWaterCommand;
}
- (RACCommand *)saveWaterComm
{
    if(!_saveWaterComm)
    {
        @weakify(self);
        _saveWaterComm = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            return  [self.netWorking.get(@"farm/detail/motifiCheck").params(self.farmLocalWaterDic).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                //NSArray<MTDynamicDataModel *> *list = [MTDynamicDataModel mj_objectArrayWithKeyValuesArray:value[@"list"]];
                //[self handlePageData:self.dataList newDatas:list];
                self.farmWaterDic = [value mutableCopy];
                [self.farmWaterDic removeObjectForKey:@"Id"];
                [self.farmWaterDic removeObjectForKey:@"farmId"];
                self.farmLocalWaterDic = [value mutableCopy];
                [self.farmLocalWaterDic setValue:value[@"Id"] forKey:@"checkId"];
                return @(YES);
            }];
            
        }];
    }
    return _saveWaterComm;
}

- (RACCommand *)farmFoodCommand
{
    if(!_farmFoodCommand)
    {
        @weakify(self);
        _farmFoodCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            NSString *farmId = input?:@"";
            return  [self.netWorking.get(@"farm/detail/foodList").params(@{@"farmId":farmId}).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                //NSArray<MTDynamicDataModel *> *list = [MTDynamicDataModel mj_objectArrayWithKeyValuesArray:value[@"list"]];
                [self handlePageData:self.foodArray newDatas:value];
//                self.foodArray = value;
                return @(YES);
            }];
            
        }];
    }
    return _farmFoodCommand;
}
- (RACCommand *)saveFoodComm
{
    if(!_saveFoodComm)
    {
        @weakify(self);
        _saveFoodComm = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            return  [self.netWorking.get(@"farm/detail/addFood").params(input).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                //NSArray<MTDynamicDataModel *> *list = [MTDynamicDataModel mj_objectArrayWithKeyValuesArray:value[@"list"]];
                //[self handlePageData:self.dataList newDatas:list];
                [self.reloadSubject sendNext:nil];
                return @(YES);
            }];
            
        }];
    }
    return _saveFoodComm;
}

- (RACCommand *)deleteFoodComm
{
    if(!_deleteFoodComm)
    {
        @weakify(self);
        _deleteFoodComm = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            NSString *foodId = input?:@"";
            return  [self.netWorking.get(@"farm/detail/deleteFood").params(@{@"foodId":foodId}).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                //NSArray<MTDynamicDataModel *> *list = [MTDynamicDataModel mj_objectArrayWithKeyValuesArray:value[@"list"]];
                //[self handlePageData:self.dataList newDatas:list];
                [self.reloadSubject sendNext:nil];
                return @(YES);
            }];
            
        }];
    }
    return _deleteFoodComm;
}

- (RACCommand *)farmDrugCommand
{
    if(!_farmDrugCommand)
    {
        @weakify(self);
        _farmDrugCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            return  [self.netWorking.get(@"farm/detail/drugList").params(@{@"farmId":input?:@""}).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                //NSArray<MTDynamicDataModel *> *list = [MTDynamicDataModel mj_objectArrayWithKeyValuesArray:value[@"list"]];
                //[self handlePageData:self.dataList newDatas:list];
                [self handlePageData:self.drugArray newDatas:value];
                return @(YES);
            }];
            
        }];
    }
    return _farmDrugCommand;
}

- (RACCommand *)saveDrugComm
{
    if(!_saveDrugComm)
    {
        @weakify(self);
        _saveDrugComm = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            return  [self.netWorking.get(@"farm/detail/addDrug").params(input).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                //NSArray<MTDynamicDataModel *> *list = [MTDynamicDataModel mj_objectArrayWithKeyValuesArray:value[@"list"]];
                //[self handlePageData:self.dataList newDatas:list];
                [self.reloadSubject sendNext:nil];
                return @(YES);
            }];
            
        }];
    }
    return _saveDrugComm;
}

- (RACCommand *)deleteDrugComm
{
    if(!_deleteDrugComm)
    {
        @weakify(self);
        _deleteDrugComm = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            @strongify(self);
            NSString *drugId = input?:@"";
            return  [self.netWorking.get(@"farm/detail/deleteDrug").params(@{@"drugId":drugId}).executeSignal.mapWithRawData map:^id _Nullable(id  _Nullable value) {
                //NSArray<MTDynamicDataModel *> *list = [MTDynamicDataModel mj_objectArrayWithKeyValuesArray:value[@"list"]];
                //[self handlePageData:self.dataList newDatas:list];
                [self.reloadSubject sendNext:nil];
                return @(YES);
            }];
            
        }];
    }
    return _deleteDrugComm;
}

- (void)getFarmListName:(NSIndexPath *)indexPath block:(void (^)(NSString * _Nonnull, NSString * _Nonnull))block
{
    if (block) {
        block(self.farmListArray[indexPath.row][@"farmname"],self.farmListArray[indexPath.row][@"Id"]);
    }
}
- (void)getFarmDataListDetail:(NSIndexPath *)indexPath block:(void(^)(NSString *date,NSString *sumDrug,NSString *sumFood,NSString *farmId))block
{
    NSDictionary *farmData = self.farmDataListArray[indexPath.row];
    NSString *date = farmData[@"Date"];
    NSString *sumdrug = [NSString stringWithFormat:@"总药量:%@",farmData[@"sumdrug"]];
    NSString *sumfood = [NSString stringWithFormat:@"总料量:%@",farmData[@"sumfood"]];
    NSString *Id = farmData[@"Id"];
    if (block) {
        block(date,sumdrug,sumfood,Id);
    }
}



- (void)getFarmEveryDay:(NSIndexPath *)indexPath block:(void (^)(NSString * _Nonnull, NSString * _Nonnull))block
{
    NSString *name = self.farmEveryDayDic.allKeys[indexPath.row];
    NSString *zhName = self.everyExName[name];
    NSString *detail = self.farmEveryDayDic[name];
    if (block) {
        block(zhName,detail);
    }
}

- (void)setEveryDatDetil:(NSIndexPath *)indexPath detail:(NSString *)detail
{
    NSString *name = self.farmEveryDayDic.allKeys[indexPath.row];
    self.farmEveryDayDic[name] = detail;
    self.farmLocalEveryDayDic[name] = detail;
}

- (void)getFarmWater:(NSIndexPath *)indexPath block:(void (^)(NSString * _Nonnull, NSString * _Nonnull))block
{
    NSString *name = self.farmWaterDic.allKeys[indexPath.row];
    NSString *zhName = self.everyExName[name];
    NSString *detail = self.farmWaterDic[name];
    if (block) {
        block(zhName,detail);
    }
}

- (void)setWaterDetil:(NSIndexPath *)indexPath detail:(NSString *)detail
{
    NSString *name = self.farmWaterDic.allKeys[indexPath.row];
    self.farmWaterDic[name] = detail;
    self.farmLocalWaterDic[name] = detail;
}

- (void)getFoodDetail:(NSIndexPath *)indexPath block:(void (^)(NSString * _Nonnull, NSString * _Nonnull, NSString * _Nonnull, NSString * _Nonnull, NSString * _Nonnull, NSString * _Nonnull, NSString * _Nonnull, NSString * _Nonnull))block
{
    NSDictionary *food = self.foodArray[indexPath.row];
    NSString *MealTime = [food[@"MealTime"] isEqualToString:@"00"]?@"早餐":[food[@"MealTime"] isEqualToString:@"01"]?@"午餐":[food[@"MealTime"] isEqualToString:@"02"]?@"晚餐":[NSString stringWithFormat:@"加餐%@",food[@"MealTime"]];
    NSString *foodname = [NSString stringWithFormat:@"料名:%@",food[@"foodname"]?:@""];
    NSString *much = [NSString stringWithFormat:@"料量:%@",food[@"much"]?:@""];
    NSString *eattime = [NSString stringWithFormat:@"食料时长:%@",food[@"eattime"]?:@""];
    NSString *createTime = [NSString stringWithFormat:@"记录时间:%@",food[@"createTime"]?:@""];
    NSString *drugname = [NSString stringWithFormat:@"伴药名称:%@",food[@"drugname"]?:@""];
    NSString *drugmuch = [NSString stringWithFormat:@"伴药量:%@",food[@"drugmuch"]?:@""];
    NSString *foodId = food[@"Id"];
    if (block) {
        block(MealTime,foodname,much,eattime,createTime,drugname,drugmuch,foodId);
    }
}

- (void)getDrugDetail:(NSIndexPath *)indexPath block:(void (^)(NSString * _Nonnull, NSString * _Nonnull, NSString * _Nonnull, NSString * _Nonnull, NSString * _Nonnull))block
{
    NSDictionary *drug = self.drugArray[indexPath.row];
    NSString *MealTime = [drug[@"MealTime"] isEqualToString:@"00"]?@"餐次:早餐":[drug[@"MealTime"] isEqualToString:@"01"]?@"餐次:午餐":[drug[@"MealTime"] isEqualToString:@"02"]?@"餐次:晚餐":[NSString stringWithFormat:@"餐次:加餐%@",drug[@"MealTime"]];
    
    NSString *drugname = [NSString stringWithFormat:@"药名:%@",drug[@"drugname"]?:@""];
    NSString *much = [NSString stringWithFormat:@"药量:%@",drug[@"much"]?:@""];
    NSString *createTime = [NSString stringWithFormat:@"记录时间:%@",drug[@"createTime"]?:@""];
    NSString *drugId = drug[@"Id"];
    if (block) {
        block(drugname,much,MealTime,createTime,drugId);
    }
}


- (void)clearDataList
{
    [self.farmDataListArray removeAllObjects];
}

- (void)clearFoodList
{
    [self.foodArray removeAllObjects];
}

- (void)clearDrugList
{
    [self.foodArray removeAllObjects];
}

- (RACSubject *)reloadSubject
{
    if (!_reloadSubject) {
        _reloadSubject = [RACSubject subject];
    }
    return _reloadSubject;
}


- (NSMutableArray *)farmListArray
{
    if (!_farmListArray) {
        _farmListArray = [NSMutableArray array];
    }
    return _farmListArray;
}
- (NSMutableArray *)farmDataListArray
{
    if (!_farmDataListArray) {
        _farmDataListArray = [NSMutableArray array];
    }
    return _farmDataListArray;
}
- (NSMutableDictionary *)farmEveryDayDic
{
    if (!_farmEveryDayDic) {
        _farmEveryDayDic = [NSMutableDictionary dictionary];
    }
    return _farmEveryDayDic;
}

- (NSMutableDictionary *)farmLocalEveryDayDic
{
    if (!_farmLocalEveryDayDic) {
        _farmLocalEveryDayDic = [NSMutableDictionary dictionary];
    }
    return _farmLocalEveryDayDic;
}

- (NSMutableArray *)foodArray
{
    if (!_foodArray) {
        _foodArray = [NSMutableArray array];
    }
    return _foodArray;
}

- (NSMutableArray *)drugArray
{
    if (!_drugArray) {
        _drugArray = [NSMutableArray array];
    }
    return _drugArray;
}

- (NSDictionary *)everyExName
{
    return @{
        @"floating":@"漂浮物",
        @"watercolor":@"水质颜色",
        @"lifting":@"起料",
        @"thinning":@"疏苗",
        @"waterTem":@"水质温度",
        @"watersmell":@"水质气味",
        @"shrimplong":@"虾长",
        @"pH":@"pH值",
        @"sulfite":@"亚盐",
        @"Ammonia":@"氨氮",
        @"alkali":@"总碱"
    };
}


@end
